<?php

namespace Home\Controller;

/**
 * APP逻辑
 *
 * @author Administrator
 *        
 */
class LobbyController extends BaseController {
	/**
	 * 获取应用信息
	 */
	public function mb_GetAppInfo() {
		$data = $this->mb_OnMessage ();
		$appInfo = M ( 'Appinfo' );
		$platform = $data->platform;
		
		$appData = $appInfo->where ( "id='$platform'" )->find ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $appData ) );
	}
	// public function mb_GetGameInfo() {
	// $gameInfo = M ( 'Gameinfo' );
	// $this->mb_OutMessage ( ErrCode_Normal, json_encode ( $gameInfo->select () ) );
	// }
	/**
	 * 获取游戏信息
	 */
	public function mb_GetGameInfo() {
		$data = $this->mb_OnMessage ();
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->select ();
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $gameData ) );
	}
	/**
	 * 登录
	 */
	public function mb_Login() {
		$data = $this->mb_OnMessage ();
		var_dump($data);exit;
		$name = $data->name;
		$encPass = $data->pass;
		
		$adminInfo = M ( 'Admininfo' );
		$userData = $adminInfo->where ( "name='$name' AND encpass='$encPass'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '您输入的账号或密码错误' );
			return;
		}
		if ($userData ['status'] != 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '登录受限，请联系上级管理员' );
			return;
		}
		
		$token = md5 ( $userData ['name'] . \Tools::mb_GetCurDateTime () );
		$adminInfo->where ( "name='$name'" )->save ( array (
				'token' => $token 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, $token );
	}
	/**
	 * 登出
	 */
	public function mb_Logout() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$name = $adminData ['name'];
		$adminInfo->where ( "name='$name'" )->save ( array (
				'token' => '' 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, $token );
	}
	/**
	 * 心跳
	 */
	public function mb_HeartBeat() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$platform = $data->platform;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$appInfo = M ( 'Appinfo' );
		$platform = $data->platform;
		
		$appData = $appInfo->where ( "id='$platform'" )->find ();
		$send = array (
				'admin' => $adminData,
				'app' => $appData 
		);
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 * Token登入
	 */
	public function mb_TokenLogin() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$this->mb_OutMessage ( ErrCode_Normal, $token );
	}
	/**
	 * 获取代理信息
	 */
	public function mb_GetAdminInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $adminData ) );
	}
	public function mb_ResetPassword() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$type = $data->type;
		$oldPass = $data->oldpass;
		$newPass = $data->newpass;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($type == 0) {
			// 修改自己密码
			if ($adminData ['encpass'] != $oldPass) {
				$this->mb_OutMessage ( ErrCode_Toast, '原密码不正确！' );
				return;
			}
			
			$adminId = $adminData ['id'];
			$adminInfo->save ( array (
					'id' => $adminId,
					'basepass' => $newPass,
					'encpass' => md5 ( $newPass . C ( 'MSG_MD5' ) ) 
			) );
		} else if ($type == 1) {
			// 修改Boss密码
			$adminName = C ( 'DEVE_NAME' );
			$adminInfo->where ( "name='$adminName'" )->save ( array (
					'basepass' => $newPass,
					'encpass' => md5 ( $newPass . C ( 'MSG_MD5' ) ) 
			) );
		} else if ($type == 2) {
			// 修改Manager密码
			$adminName = C ( 'MANA_NAME' );
			$adminInfo->where ( "name='$adminName'" )->save ( array (
					'basepass' => $newPass,
					'encpass' => md5 ( $newPass . C ( 'MSG_MD5' ) ) 
			) );
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	// /////////////////////////////////////////大厅接口////////////////////////////////////////////////
	public function mb_CheckWeChatAccount() {
		$data = $this->mb_OnMessage ();
		$openId = $data->openid;
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "openid='$openId'" )->find ();
		
		$data = new \stdClass();
		if (isset ( $userData )) {
			$data->result = true;
			$data->message = '该玩家已登录过';
		}else {
			$data->result = false;
			$data->message = '新用户，请输入邀请码';
		}
		$this->mb_OutMessage(ErrCode_Normal, json_encode($data));
	}
	public function mb_WeChatRegist(){
		$data = $this->mb_OnMessage ();
		$nick = $data->nick;
		$head = $data->head;
		$sex = $data->sex;
		$openId = $data->openid;
		$unionId = $data->unionid;
		$code = $data->code;
		
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "code='$code'" )->find ();
		if (isset ( $adminData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该邀请码无效' );
			return;
		}
		if ($adminData ['level'] != 2 && $adminData ['level'] != 3) {
			$this->mb_OutMessage ( ErrCode_Toast, '该邀请码无效' );
			return;
		}
		if ($adminData ['status'] != 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '该邀请码无效' );
			return;
		}
		
		$id = $this->mb_GetIdleId ();
		$name = $this->mb_GetIdleName();
		
		$userInfo = M ( 'Userinfo' );
		$userInfo->add ( array (
				'id' => $id,
				'name' => $name,
				'basepass' => '',
				'nick' => $nick,
				'head'=>$head,
				'sex'=>$sex,
				'openid'=>$openId,
				'unionid'=>$unionId,
				'code' => $code,
				'phone' => $phone
		) );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	public function mb_WeChatLogin(){
		$data = $this->mb_OnMessage ();
		
		$nick = $data->nick;
		$head = $data->head;
		$sex = $data->sex;
		$openId = $data->openid;
		$unionId = $data->unionid;
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "openid='$openId'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		if ($userData ['status'] == 1) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号状态异常' );
			return;
		} else if ($userData ['status'] == 2) {
			$this->mb_OutMessage ( ErrCode_Toast, '服务器已关闭' );
			return;
		}
		
		$curTime = \Tools::mb_GetCurDateTime ();
		$userInfo->save ( array (
				'id' => $userData ['id'],
				'login' => $curTime
		) );
		$userData ['login'] = $curTime;
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	
	/**
	 * 账号注册
	 */
	public function mb_AccountRegist() {
		$data = $this->mb_OnMessage ();
		$name = $data->name;
		$nick = $name;
		$pass = $data->pass;
		$code = $data->code;
		$phone = $data->phone;
		
		if ($name == 'mpzz' || $name == 'mpzz2' || $name == 'videobackming1' || $name == 'videobackhlsg' || $name == 'videobackbjl') {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号已注册' );
			return;
		}
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData )) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号已注册' );
			return;
		}
		
		if ($phone != '') {
			$userData = $userInfo->where ( "phone='$phone'" )->find ();
			if (isset ( $userData )) {
				$this->mb_OutMessage ( ErrCode_Toast, '该手机号已注册' );
				return;
			}
		}
		
		$id = $this->mb_GetIdleId ();
		$adminInfo = M ( 'Admininfo' );
		$adminData = $adminInfo->where ( "code='$code'" )->find ();
		if (isset ( $adminData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该邀请码无效' );
			return;
		}
		if ($adminData ['level'] != 2 && $adminData ['level'] != 3) {
			$this->mb_OutMessage ( ErrCode_Toast, '该邀请码无效' );
			return;
		}
		if ($adminData ['status'] != 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '该邀请码无效' );
			return;
		}
		
		$userInfo->add ( array (
				'id' => $id,
				'name' => $name,
				'basepass' => $pass,
				'nick' => $nick,
				'code' => $code,
				'phone' => $phone 
		) );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 帐号登录
	 */
	public function mb_AccountLogin() {
		$data = $this->mb_OnMessage ();
		$name = $data->name;
		$pass = $data->pass;
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		if ($userData ['status'] == 1) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号状态异常' );
			return;
		} else if ($userData ['status'] == 2) {
			$this->mb_OutMessage ( ErrCode_Toast, '服务器已关闭' );
			return;
		}
		
		$curTime = \Tools::mb_GetCurDateTime ();
		$userInfo->save ( array (
				'id' => $userData ['id'],
				'login' => $curTime 
		) );
		$userData ['login'] = $curTime;
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 帐号查询
	 */
	public function mb_AccountQuery() {
		$data = $this->mb_OnMessage ();
		$name = $data->name;
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 帐号保存
	 */
	public function mb_AccountSave() {
		$data = $this->mb_OnMessage ();
		$name = $data->name;
		$coin = $data->coin;
		$gameId = $data->gameid;
		$remark = $data->remark;
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		$id = $userData ['id'];
		$bCoin = $userData ['coin'];
		$lCoin = $bCoin + $coin;
		
		if ($lCoin < 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号金币不足' );
			return;
		}
		$lCoin = round ( $lCoin, 2 );
		$userDataLog = M ( 'Userdatalog' );
		
		$userInfo->save ( array (
				'id' => $id,
				'coin' => $lCoin 
		) );
		
		$userDataLog->add ( array (
				'name' => $name,
				'beforecoins' => $bCoin,
				'coins' => $coin,
				'latercoins' => $lCoin,
				'gameid' => $gameId,
				'remark' => $remark 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 重设密码
	 */
	public function mb_AccountResetPass() {
		$data = $this->mb_OnMessage ();
		$name = $data->name;
		$pass = $data->pass;
		
		$userInfo = M ( 'Userinfo' );
		$userData = $userInfo->where ( "name='$name'" )->find ();
		if (isset ( $userData ) == false) {
			$this->mb_OutMessage ( ErrCode_Toast, '该帐号不存在' );
			return;
		}
		$id = $userData ['id'];
		
		$userInfo->save ( array (
				'id' => $id,
				'basepass' => $pass 
		) );
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 */
	public function mb_GetGameConfig() {
		$data = $this->mb_OnMessage ();
		$gameId = $data->gameid;
		if ($gameId == 1) {
			$reqObj = new \Req_sfsFSZZ ();
		} else if ($gameId == 2) {
			$reqObj = new \Req_sfsHLSG ();
		} else if ($gameId == 4) {
			$reqObj = new \Req_sfsMPZZ ();
		} else if ($gameId == 5) {
			$reqObj = new \Req_sfsBJL ();
		} else if ($gameId == 13) {
			$reqObj = new \Req_sfsMPZZ2 ();
		} else if ($gameId == 14)
			$reqObj = new \Req_sfsSWZZ ();
		// $reqObj = new \Req_sfsMPZZ ();
		$reqData = $this->mb_DecodeSFSData ( $reqObj->GetConfig () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data;
		
		$name = $data->FJM;
		$limit = $data->XH;
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'name' => $name,
				'limit' => $limit 
		) ) );
	}
	private function mb_DecodeSFSData($reqData) {
		$data = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$state = $reqJson->state;
		if ($state == false) {
			$data->state = false;
			$data->msg = $reqJson->msg;
			return $data;
		}
		$data->state = true;
		if (isset ( $reqJson->msg ))
			$data->msg = $reqJson->msg;
		$data->data = $reqJson;
		return $data;
	}
	/**
	 * 获取闲置id号
	 *
	 * @return number
	 */
	private function mb_GetIdleId() {
		$userInfo = M ( 'Userinfo' );
		$id = mt_rand ( 100000, 999999 );
		while ( true ) {
			$userData = $userInfo->where ( "id='$id'" )->find ();
			if (isset ( $userData )) {
				$id = mt_rand ( 100000, 999999 );
			} else {
				break;
			}
		}
		return $id;
	}
	/**
	 * 获取闲置id号
	 *
	 * @return number
	 */
	private function mb_GetIdleName() {
		$userInfo = M ( 'Userinfo' );
		$name = 'wx'.\Tools::mb_RandStr(4);
		while ( true ) {
			$userData = $userInfo->where ( "id='$id'" )->find ();
			if (isset ( $userData )) {
				$name = 'wx'.\Tools::mb_RandStr(4);
			} else {
				break;
			}
		}
		return $name;
	}
	// /////////////////////////////////////////游戏接口////////////////////////////////////////////////
}